import O1HD, {modules} from "../engines/gameEngine";
import {REWARD_TYPE} from "../engines/services/userContent/userInterface";
import gameConfig from "../engines/configs/gameConfig";
import data_control from "../engines/libs/data_control";

const {ccclass, property} = cc._decorator;

@ccclass
export default class InfinityPower extends cc.Component {

    @property(cc.Node)
    detail: cc.Node = null;

    @property(cc.Label)
    looktimeLabel: cc.Label = null;

    @property(cc.Label)
    lbMessageExplain: cc.Label = null;

    @property({type:cc.Label,displayName:"获取信息展示"})
    private showInfoLabel : cc.Label = null;

    @property(cc.Prefab)
    RewardView: cc.Prefab = null;

    @property(cc.Node)
    Reward: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:

    private static lookedTimes: number = 0

    onLoad() {
        this.UpdateLookLabel()
        this.node.zIndex = cc.macro.MAX_ZINDEX;
    }

    protected start(): void {
        modules().uiControlModule.showHideDialogAnimation(this.detail, true);
        this.showInfoLabel.string =`欣赏${gameConfig.INFINTY_VIDEO_TIME}次视频获取${data_control.MinusToHours(gameConfig.INFINITY_POWER_VIDEO_HAND_TIME)}无限体力`; //this.lbMessageExplain.string.replace(/#/g, `${gameConfig.INFINITY_POWER_VIDEO_COUNT}`);
    }

    OnNoLookBtn() {
        O1HD.getManagers().soundMgr.playSound('mainSound/anNiu').then();
        this.onClose();

    }

    OnLookBtn() {
        O1HD.getManagers().soundMgr.playSound('mainSound/anNiu').then();
        if (InfinityPower.lookedTimes < gameConfig.INFINTY_VIDEO_TIME) {
            O1HD.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(() => {
                    InfinityPower.lookedTimes++
                    this.UpdateLookLabel()
                    if (InfinityPower.lookedTimes == gameConfig.INFINTY_VIDEO_TIME) {
                        //进入无限体力模式
                        O1HD.getServices().userSrv.setPower(999, true, true)
                        // O1HD.getServices().localStorageSrv.setStorage(this.InfinityModel, 1)
                        O1HD.getModules().uiControlModule.showGetRewarded(REWARD_TYPE.POWER, 999, '获得体力');
                        this.onClose();
                    }
                }, () => {
                },
                null,
                '解锁无限体力'
            )
        }
    }

    onClose() {
        modules().uiControlModule.showHideDialogAnimation(this.detail, false, () => {
            this.node.destroy();
        })
    }

    UpdateLookLabel() {
        if (this.looktimeLabel) {
            this.looktimeLabel.string = `(${InfinityPower.lookedTimes.toString()}/${gameConfig.INFINTY_VIDEO_TIME})`;
        }
    }

    // update (dt) {}
}
